package MyGO.controller;

import MyGO.Data.GlobalData;
import MyGO.Form.TrackForm;
import MyGO.property.CheckPro;
import MyGO.service.ICheackProService;
import MyGO.service.impl.CheckProServiceImpl;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import lombok.Setter;

import java.util.List;

import static MyGO.utils.ArrayfilterUtils.ArrayfilterUtils.FilterCheackProByIDAndPatientName;
import static MyGO.utils.ArrayfilterUtils.ArrayfilterUtils.FilterTrackFormByIDAndPatientName;

public class CheckDoctorViewController {
    @Setter
    private String filtDept;

    public CheckDoctorViewController(String filtDept) {
        this.filtDept = filtDept;
    }
    @FXML
    private Label PIDW;
    @FXML
    private Label PageW;
    @FXML
    private Label PgenderW;
    @FXML
    private Label PnameW;
    @FXML
    private TextField aimW;
    @FXML
    private TextField checkPartW;
    @FXML
    private TextField remarkW;


    @FXML
    private TextField ID_Search;
    @FXML
    private TextField checkName_Search;
    @FXML
    private TableColumn<CheckPro, String> IDCloumn;
    @FXML
    private TableColumn<CheckPro, String> checkNameCloumn;
    @FXML
    private TableColumn<CheckPro, Number> codeCloumn;
    @FXML
    private TableColumn<CheckPro, String> priceCloumn;
    @FXML
    private TableColumn<CheckPro, String> specCloumn;
    @FXML
    private Label label_way;
    @FXML
    private Button search;
    @FXML
    private Button summit;
    @FXML
    private Button clearForm;
    @FXML
    private HBox PatientInfo;

    @FXML
    private TableView<CheckPro> tv_CheckPro;

    private StringProperty tf_searchID = new SimpleStringProperty();
    private StringProperty tf_searchCheckName = new SimpleStringProperty();
    private ICheackProService cheackProService = new CheckProServiceImpl();

    @FXML
    public void initialize() {
//        PnameW.setText(GlobalData.trackForm.getName());
//        PIDW.setText(GlobalData.trackForm.getId());
//        PageW.setText(String.valueOf(GlobalData.trackForm.getPatientInfoForm().getYear()));
//        PgenderW.setText(GlobalData.trackForm.getPatientInfoForm().getGender());


        List<CheckPro> checkPros = cheackProService.selectCheckProByDept(this.filtDept);

        IDCloumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        codeCloumn.setCellValueFactory(cellData ->
                new SimpleIntegerProperty(tv_CheckPro.getItems().indexOf(cellData.getValue()) + 1));
        checkNameCloumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        specCloumn.setCellValueFactory(new PropertyValueFactory<>("specific"));
        priceCloumn.setCellValueFactory(new PropertyValueFactory<>("priceStr"));
        tv_CheckPro.getItems().addAll(checkPros);

        ID_Search.textProperty().bindBidirectional(tf_searchID);
        checkName_Search.textProperty().bindBidirectional(tf_searchCheckName);

        tf_searchID.addListener((observable, oldValue, newValue) -> {
//            List<TrackForm> lts = FilterByTrackFormID(trackForms, newValue);
            String nameValue = tf_searchCheckName.getValue();
            List<CheckPro> lts = FilterCheackProByIDAndPatientName(checkPros, newValue, nameValue);
            if (lts != null) {
                // 删除原有数据
                tv_CheckPro.getItems().clear();
                // 填充数据
                tv_CheckPro.getItems().addAll(lts);
            }
        });

        tf_searchCheckName.addListener((observable, oldValue, newValue) -> {
            String IDValue = tf_searchID.getValue();
//            List<TrackForm> lns = trackFormService.selectTrackFormByName(newValue);
            List<CheckPro> lns = FilterCheackProByIDAndPatientName(checkPros, IDValue, newValue);
            System.out.println(newValue);
            if (lns != null) {
                // 删除原有数据
                tv_CheckPro.getItems().clear();
                // 填充数据
                tv_CheckPro.getItems().addAll(lns);
            }
        });

    }


    @FXML
    void tf_searchNameReleased(KeyEvent event) {
        if (event.getCode().equals(KeyCode.DOWN)) {
            // 表格获取焦点
            tv_CheckPro.requestFocus(); // 设置焦点，生成视觉效果，获取键盘输入
            tv_CheckPro.getSelectionModel().selectFirst();  // 调用TableView的选择模型，并选中第一行
        }
        if (event.getCode().equals(KeyCode.UP)) {
            if (tv_CheckPro.getSelectionModel().getSelectedIndex() == 0) {
                checkName_Search.requestFocus();
                checkName_Search.selectAll();
            }
        }
    }

    @FXML
    void tf_searchIDReleased(KeyEvent event) {
        if (event.getCode().equals(KeyCode.DOWN)) {
            // 表格获取焦点
            tv_CheckPro.requestFocus(); // 设置焦点，生成视觉效果，获取键盘输入
            tv_CheckPro.getSelectionModel().selectFirst();  // 调用TableView的选择模型，并选中第一行
        }
        if (event.getCode().equals(KeyCode.UP)) {
            if (tv_CheckPro.getSelectionModel().getSelectedIndex() == 0) {
                ID_Search.requestFocus();
                ID_Search.selectAll();
            }
        }
    }


    @FXML
    void tv_ClickCheckProReleased(MouseEvent event) {
        tv_CheckPro.requestFocus();
    }

    @FXML
    void tv_searchCheckProReleased(KeyEvent event) {
        if (event.getCode().equals(KeyCode.ENTER)) {
            CheckPro checkPro = tv_CheckPro.getSelectionModel().getSelectedItem();
            GlobalData.trackForm.getCheckForm().addCheck(checkPro);
            System.out.println(checkPro);
        }
    }

    @FXML
    void ClearForm(ActionEvent event) {
        aimW.clear();
        checkPartW.clear();
        remarkW.clear();
    }

    @FXML
    void Summit(ActionEvent event) {
        sure();
    }

    void sure() {
        GlobalData.trackForm.getCheckForm().setAim(aimW.getText());
        GlobalData.trackForm.getCheckForm().setCheckPart(checkPartW.getText());
        GlobalData.trackForm.getCheckForm().setRemark(remarkW.getText());
        aimW.clear();
        checkPartW.clear();
        remarkW.clear();
        GlobalData.trackForm.setStatus_main("未缴费");
    }
}
